package bai7.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;

import bai7.model.Employee;
import bai7.service.implement.EmployeeAPI;

public class EmployeeDAO implements EmployeeAPI {
	Scanner ip=new Scanner(System.in);
	ArrayList<Employee> list=new ArrayList<Employee>();
	public void inputEmployee() {
		try{
			System.out.println("Nhap so Employee can them");

			int n=Integer.parseInt(ip.nextLine());
			for(int i=1;i<=n;i++){
				System.out.println("Nhap thong tin Employee thu "+i);
				System.out.println("Nhap id: ");
				int id=Integer.parseInt(ip.nextLine());
				System.out.println("Nhap name: ");
				String name=ip.nextLine();
				if(name.trim().equalsIgnoreCase("")){
					System.out.println("Name chua duoc nhap");
					name=ip.nextLine();
				}
				System.out.println("Nhap DateofBirth: ");
				String dateOfbirth=ip.nextLine();
				if(dateOfbirth.trim().equalsIgnoreCase("")){
					System.out.println("Date chua duoc nhap");
					dateOfbirth=ip.nextLine();
				}
				if(!checkDate(dateOfbirth)){
					System.out.println("Date nhap vao khong dung dinh dang");
					dateOfbirth=ip.nextLine();
				}
				System.out.println("Nhap dia chi: ");
				String dc=ip.nextLine();
				if(dc.trim().equalsIgnoreCase("")){
					System.out.println("Dia chi chua duoc nhap");
					dc=ip.nextLine();
				}
				System.out.println("Nhap email: ");
				String email=ip.nextLine();
				if(email.trim().equalsIgnoreCase("")){
					System.out.println("Email chua duoc nhap");
					email=ip.nextLine();
				}
				System.out.println("Nhap luong: ");
				int luong=Integer.parseInt(ip.nextLine());
				Employee emp=new Employee(name,dc,dateOfbirth,email,id,luong);
				list.add(emp);}
		}catch(NumberFormatException e){
			System.out.println("NHap khong dung dinh dang");
		}
	}
	public void deleteEmployee(int id) {
		list.remove(id-1);
	}
	public void display() {
		System.out.println("============================Danh sach Employee==============================\n");
		System.out.println("|id\t|Name\t\t\t|DateofBirth\t|Dia chi\t|Email\t\t\t|Luong\n");
		for(Employee emp:list){
			System.out.println("|"+emp.getId()+"\t|"+emp.getName()+"\t\t|"+emp.getDateOfbirth()+"\t|"+emp.getDc()+"\t\t|"+emp.getEmail()+"\t|"+emp.getLuong());
			System.out.println("----------------------------------------------------------------------------------");
		}	
	}
	public void searchEmployee(int id) {
		//		String a=Integer.toString(id);
		for(Employee emp:list){
			if(emp.getId()==id){
				System.out.println("|id\t|Name\t\t\t|DateofBirth\t|Email\t\t\t|Luong\n");
				System.out.println("|"+emp.getId()+"\t|"+emp.getName()+"\t\t|"+emp.getDateOfbirth()+"\t|"+emp.getEmail()+"\t|"+emp.getLuong());
			}
			else{
				System.out.println("Khong tim thay id ");
			}
		}	
	}
	public boolean checkDate(String date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		try {
			dateFormat.setLenient(false);
			dateFormat.parse(date);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
